package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ScoreContentListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.StaffscoreTypeContent;
import com.isunland.managesystem.entity.StaffscoreTypeContent.ScoreTypeContent;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  奖惩内容列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2016/5/12 18:30
 */
public class ScoreContentListFragment extends BaseListFragment {

  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ScoreContentListFragment.EXTRA_VALUE";
  public static final String EXTRA_DEPTCODE =
      "com.isunland.managesystem.ui.ScoreContentListFragment.EXTRA_DEPTCODE";
  private ScoreContentListAdapter mAdapter;
  private String mDeptCode;
  private CurrentUser mCurrentUser;
  private int REQUEST_QUERY = 1;
  private String rewardKindCode;
  private String rewardKindName;
  private String rewardContentLike;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.scoreTypeContent);
    mCurrentUser = CurrentUser.newInstance(getActivity());
    mDeptCode = getArguments().getString(EXTRA_DEPTCODE);
    rewardKindCode = "";
    rewardKindName = "";
    rewardContentLike = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public String getUrl() {
    return ApiConst.uRL_SCORE_TYPE_CONTENT;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("dataStatus", "publish");
    params.put("orderField", "reward_kind_code");
    params.put("orderSeq", "asc");
    params.put("page", "1");
    params.put("pageSize", "1000");
    params.put("isPaging", "0");
    params.put("deptCode", mCurrentUser.getDeptCode());
    params.put("acceptDeptCodes", mDeptCode);
    params.put("rewardContentLike", rewardContentLike);
    params.put("rewardKindCode", rewardKindCode);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    StaffscoreTypeContent typeContent = new Gson().fromJson(json, StaffscoreTypeContent.class);
    List<ScoreTypeContent> content = typeContent.getRows();
    if (content != null && content.size() == 0) {
      ToastUtil.show(R.string.that_is_all);
    }
    setAdapter(content);
  }

  private void setAdapter(List<ScoreTypeContent> content) {
    if (content == null) {
      content = new ArrayList<>();
    }

    if (mAdapter == null) {
      mAdapter = new ScoreContentListAdapter(getActivity(), content);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(content);
    ((ScoreContentListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ScoreTypeContent content = mAdapter.getItem(position - 1);
    Intent intent = new Intent();
    intent.putExtra(EXTRA_VALUE, content);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_query:
        //查询界面
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(rewardKindName);
        queryConten.setExtCode1(rewardKindCode);
        queryConten.setExtName2(rewardContentLike);
        Intent query = new Intent(getActivity(), ScoreContentQueryActivity.class);
        query.putExtra(ScoreContentQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public static Fragment newInstance(String deptCode) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_DEPTCODE, deptCode);
    ScoreContentListFragment fragment = new ScoreContentListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(ScoreContentQueryFragment.EXTRA_CONTENT);
      rewardKindName = queryConten.getExtName1();
      rewardKindCode = queryConten.getExtCode1();
      rewardContentLike = queryConten.getExtName2();
    }
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
